package com.ruoyi.crm.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.crm.domain.CrmContract;
import com.ruoyi.crm.domain.CrmCustomer;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.domain.bo.MaskBo;
import com.ruoyi.crm.mapper.CrmContractMapper;
import com.ruoyi.crm.mapper.CrmCustomerMapper;
import com.ruoyi.crm.mapper.CrmOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.CrmProductMapper;
import com.ruoyi.crm.domain.CrmProduct;
import com.ruoyi.crm.service.ICrmProductService;

/**
 * 产品列表Service业务层处理
 *
 * @author yanwenliang
 * @date 2024-10-24
 */
@Service
public class CrmProductServiceImpl extends ServiceImpl<CrmProductMapper, CrmProduct> implements ICrmProductService
{
    @Autowired
    private CrmProductMapper crmProductMapper;

    @Autowired
    private CrmCustomerMapper crmCustomerMapper;

    @Autowired
    private CrmOrderMapper crmOrderMapper;

    @Autowired
    private CrmContractMapper crmContractMapper;

    /**
     * 查询产品列表
     *
     * @param productId 产品列表主键
     * @return 产品列表
     */
    @Override
    public CrmProduct selectCrmProductByProductId(String productId)
    {
        return crmProductMapper.selectCrmProductByProductId(productId);
    }

    /**
     * 查询产品列表列表
     *
     * @param crmProduct 产品列表
     * @return 产品列表
     */
    @Override
    public List<CrmProduct> selectCrmProductList(CrmProduct crmProduct) {
        List<CrmProduct> crmProducts = crmProductMapper.selectCrmProductList(crmProduct);
        crmProducts.forEach(item->{
            List<CrmOrder> crmOrders = crmOrderMapper.selectList(new LambdaQueryWrapper<CrmOrder>().eq(CrmOrder::getProductId, item.getProductId()));
            List<MaskBo> bos = new ArrayList<>();
            if(StringUtils.isNotEmpty(crmOrders)){
                crmOrders.forEach(crmOrder -> {
                    CrmContract crmContract = crmContractMapper.selectOne(new LambdaQueryWrapper<CrmContract>().eq(CrmContract::getOrderId, crmOrder.getOrderId()));
                    if(ObjectUtil.isNotNull(crmContract)){
                        CrmCustomer crmCustomer = crmCustomerMapper.selectById(crmOrder.getCustomerId());
                        MaskBo bo = new MaskBo();
                        bo.setCustomerName(crmCustomer.getCustomerName());
                        bo.setMakeMoney(crmContract.getTotalMoney());
                        bo.setMakeDate(crmContract.getTotalTime());
                        bos.add(bo);
                    }
                });
            }
            item.setBos(bos);
        });
        return crmProducts;
    }

    /**
     * 新增产品列表
     *
     * @param crmProduct 产品列表
     * @return 结果
     */
    @Override
    public int insertCrmProduct(CrmProduct crmProduct) {
        crmProduct.setCreateTime(DateUtils.getNowDate());
        crmProduct.setCreateBy(SecurityUtils.getUsername());
        crmProduct.setProductId(IdWorker.getIdStr());
        return crmProductMapper.insertCrmProduct(crmProduct);
    }

    /**
     * 修改产品列表
     *
     * @param crmProduct 产品列表
     * @return 结果
     */
    @Override
    public int updateCrmProduct(CrmProduct crmProduct)
    {
        crmProduct.setUpdateTime(DateUtils.getNowDate());
        return crmProductMapper.updateCrmProduct(crmProduct);
    }

    /**
     * 批量删除产品列表
     *
     * @param productIds 需要删除的产品列表主键
     * @return 结果
     */
    @Override
    public int deleteCrmProductByProductIds(String[] productIds)
    {
        return crmProductMapper.deleteCrmProductByProductIds(productIds);
    }

    /**
     * 删除产品列表信息
     *
     * @param productId 产品列表主键
     * @return 结果
     */
    @Override
    public int deleteCrmProductByProductId(String productId)
    {
        return crmProductMapper.deleteCrmProductByProductId(productId);
    }
}
